package de.derhess.projects.as3_harmony_port
{
	import com.bit101.components.ColorChooser;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;
	import com.bit101.components.VBox;
	import com.bit101.components.Window;
	import com.flashdynamix.utils.SWFProfiler;
	
	import de.derhess.drawing.*;
	import de.derhess.drawing.brushes.AbstractBrush;
	import de.derhess.drawing.brushes.ChromeBrush;
	import de.derhess.drawing.brushes.CirclesBrush;
	import de.derhess.drawing.brushes.FanBrush;
	import de.derhess.drawing.brushes.FurBrush;
	import de.derhess.drawing.brushes.GridBrush;
	import de.derhess.drawing.brushes.IBrush;
	import de.derhess.drawing.brushes.LongFurBrush;
	import de.derhess.drawing.brushes.RibbonBrush;
	import de.derhess.drawing.brushes.ShadedBrush;
	import de.derhess.drawing.brushes.SimpleBrush;
	import de.derhess.drawing.brushes.SketchyBrush;
	import de.derhess.drawing.brushes.SquaresBrush;
	import de.derhess.drawing.brushes.WebBrush;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	[SWF(width="1200", height="650", frameRate="30", backgroundColor="#FFFFFF")]
	public class as3_harmony extends Sprite
	{
	
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var simpleBrush:SimpleBrush;
		private var chromeBrush:ChromeBrush;
		private var shadedBrush:ShadedBrush;
		private var sketchyBrush:SketchyBrush;
		private var webBrush:WebBrush;
		private var furBrush:FurBrush;
		private var longFurBrush:LongFurBrush;
		private var gridBrush:GridBrush;
		private var circleBrush:CirclesBrush;
		private var squareBrush:SquaresBrush;
		private var ribbonBrush:RibbonBrush;
		private var fanBrush:FanBrush;
		
		private var brush:IBrush;
		private var canvas:Sprite;
		private var background:Sprite;
		private var bmpCanvas:BitmapData;
		private var bitmap:Bitmap;
		private var isDrawing:Boolean;
		// GUI Elements
		public var colorPicker:ColorChooser;
		public var simpleRadio:RadioButton;
		public var chromeRadio:RadioButton;
		public var shadedRadio:RadioButton;
		public var sketchyRadio:RadioButton;
		public var webRadio:RadioButton;
		public var furRadio:RadioButton;
		public var longFurRadio:RadioButton;
		public var gridRadio:RadioButton;
		public var circleRadio:RadioButton;
		public var squaresRadio:RadioButton;
		public var fanRadio:RadioButton;
		public var ribbonRadio:RadioButton;
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function as3_harmony()
		{
			SWFProfiler.init(this.stage, this);
			
			//////////////////////////////////////////////////////
			// Background Stage
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(0,0,1200,650);
			this.graphics.endFill();
			
			// create Bitmap for storing the drawing
			bmpCanvas = new BitmapData(1180,630,false,0xFFEEEEEE);
			bitmap = new Bitmap(bmpCanvas,"auto",true);
			bitmap.x = 10;
			bitmap.y = 10;
			addChild(bitmap);
			
			//canvas for frame drawing
			this.canvas = new Sprite();
			this.canvas.x = bitmap.x;
			this.canvas.y = bitmap.y;
			
			// background of the canvas and important for getting MouseEvent data
			background = new Sprite();
			background.x = 10;
			background.y = 10;
			background.graphics.beginFill(0xEEEEEE);
			background.graphics.drawRect(0,0,1180,630);
			background.graphics.endFill();
			addChild(background);
			// Event Management
			background.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			background.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			background.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			background.addEventListener(MouseEvent.CLICK,handleMouseClick);
			
			// Set up the canvas
			bmpCanvas.draw(this.canvas);
			addChild(this.canvas);
			swapChildren(background,bitmap);
			isDrawing = false;
			
			///////////////////////
			// create GUIs and Brushes
			createBrushes();
			createGUI();

			brush = simpleBrush;

			trace("Hello World");
		}
		
		public function createBrushes():void
		{
			simpleBrush = new SimpleBrush(canvas);
			chromeBrush = new ChromeBrush(canvas);
			shadedBrush = new ShadedBrush(canvas);
			sketchyBrush = new SketchyBrush(canvas);
			webBrush = new WebBrush(canvas);
			furBrush = new FurBrush(canvas);
			longFurBrush = new LongFurBrush(canvas);
			gridBrush = new GridBrush(canvas);
			circleBrush = new CirclesBrush(canvas);
			squareBrush = new SquaresBrush(canvas,0x555555);
			ribbonBrush = new RibbonBrush(canvas,this.stage.width / 2, this.stage.height);
			fanBrush = new FanBrush(canvas,this.stage.width / 2, this.stage.height);
		}
		
		public function createGUI():void
		{
			colorPicker = new ColorChooser(this,10,10,0x000000,handleColorPicker);
			colorPicker.usePopup = true;
			colorPicker.popupAlign = ColorChooser.BOTTOM;
			var brushPanel:Window = new Window(this,20,20,"Drawing Styles");
			brushPanel.addChild(colorPicker);
			
			simpleRadio = new RadioButton(brushPanel.content,15,40,"simple",true,clickSimpleRadio);
			simpleRadio.groupName = "brushes";
			brushPanel.addChild(simpleRadio);
			chromeRadio = new RadioButton(brushPanel.content,15,55,"chrome",false,clickChromeRadio);
			chromeRadio.groupName = "brushes";
			brushPanel.addChild(chromeRadio);
			shadedRadio = new RadioButton(brushPanel.content,15,70,"shaded",false,clickShadedRadio);
			shadedRadio.groupName = "brushes";
			brushPanel.addChild(shadedRadio);
			sketchyRadio = new RadioButton(brushPanel.content,15,85,"sketchy",false,clickSketchyRadio);
			sketchyRadio.groupName = "brushes";
			brushPanel.addChild(sketchyRadio);
			webRadio = new RadioButton(brushPanel.content,15,100,"web",false,clickWebRadio);
			webRadio.groupName = "brushes";
			brushPanel.addChild(webRadio);
			furRadio = new RadioButton(brushPanel.content,15,115,"fur",false,clickFurRadio);
			furRadio.groupName = "brushes";
			brushPanel.addChild(furRadio);
			longFurRadio = new RadioButton(brushPanel.content,15,130,"long Fur",false,clickLongFurRadio);
			longFurRadio.groupName = "brushes";
			brushPanel.addChild(longFurRadio);
			gridRadio = new RadioButton(brushPanel.content,15,145,"grid",false,clickGridRadio);
			gridRadio.groupName = "brushes";
			brushPanel.addChild(gridRadio);
			circleRadio = new RadioButton(brushPanel.content,15,160,"circle",false,clickCircleRadio);
			circleRadio.groupName = "brushes";
			brushPanel.addChild(circleRadio);
			squaresRadio = new RadioButton(brushPanel.content,15,175,"squares",false,clickSquaresRadio);
			squaresRadio.groupName = "brushes";
			brushPanel.addChild(squaresRadio);
			fanRadio = new RadioButton(brushPanel.content,15,190,"fan",false,clickFanRadio);
			fanRadio.groupName = "brushes";
			brushPanel.addChild(fanRadio);
			ribbonRadio = new RadioButton(brushPanel.content,15,205,"ribbon",false,clickRibbonRadio);
			ribbonRadio.groupName = "brushes";
			brushPanel.addChild(ribbonRadio);
	
			var clearBtn:PushButton = new PushButton(brushPanel.content,5,225,"clear",clickClearButton);
			brushPanel.addChild(clearBtn);
			brushPanel.height = 280;
			brushPanel.width = clearBtn.width + 10;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		public function handleMouseDown(e:MouseEvent):void
		{
			trace("Mouse Down");
			brush.strokeStart(canvas.mouseX,canvas.mouseY);
			isDrawing = true;
		}
		
		public function handleMouseMove(e:MouseEvent):void
		{
			trace("Mouse Move");
			
			if(isDrawing) {
				// draw the stroke for one(!) frame
				brush.stroke(e.target.mouseX,e.target.mouseY);
				// save the drawing in a bitmapData Object for performance
				bmpCanvas.draw(canvas);
				// clean the canvas again for new drawings. Too much drawing decrease the performance very fast
				canvas.graphics.clear();
			}
		}
		
		public function handleMouseUp(e:MouseEvent):void
		{
			trace("Mouse Up");	
			brush.strokeEnd();
			isDrawing = false;
		
		}
		
		public function handleMouseClick(e:MouseEvent):void
		{
			trace("MouseClick");
		}
		
		/////////////////////////////////////////////////
		//  GUI HANDLING
		/////////////////////////////////////////////////
		public function clickClearButton(e:Event):void
		{
			trace("clickClearButton");
			simpleBrush.destroy();
			simpleBrush.init();
			chromeBrush.destroy();
			chromeBrush.init();
			shadedBrush.destroy();
			shadedBrush.init();
			sketchyBrush.destroy();
			sketchyBrush.init();
			webBrush.destroy();
			webBrush.init();
			furBrush.destroy();
			furBrush.init();
			longFurBrush.destroy();
			longFurBrush.init();
			gridBrush.destroy();
			gridBrush.init();
			circleBrush.destroy();
			circleBrush.init();
			squareBrush.destroy();
			squareBrush.init();
			ribbonBrush.destroy();
			ribbonBrush.init();
			fanBrush.destroy();
			fanBrush.init();
			
			bmpCanvas.draw(this.background);
		}
		
		public function handleColorPicker(e:Event):void
		{
			trace("handleColorPicker");
			setColor();
			
		}
		
		public function clickSimpleRadio(e:Event):void
		{
			trace("clickSimpleRadio");
			brush = simpleBrush;
			setColor();
		}
		
		public function clickChromeRadio(e:Event):void
		{
			trace("clickChromeRadio");
			brush = chromeBrush;
			setColor();
		}
		
		public function clickShadedRadio(e:Event):void
		{
			trace("clickShadedRadio");
			brush = shadedBrush;
			setColor();
		}
		
		public function clickSketchyRadio(e:Event):void
		{
			trace("clickSketchyRadio");
			brush = sketchyBrush;
			setColor();
		}
		
		public function clickWebRadio(e:Event):void
		{
			trace("clickWebRadio");
			brush = webBrush;
			setColor();
		}
		
		public function clickFurRadio(e:Event):void
		{
			trace("clickFurRadio");
			brush = furBrush;
			setColor();
		}
		
		public function clickLongFurRadio(e:Event):void
		{
			trace("clickLongFurRadio");
			brush = longFurBrush;
			setColor();
		}
		
		public function clickGridRadio(e:Event):void
		{
			trace("clickGridRadio");
			brush = gridBrush;
			setColor();
		}
		
		public function clickCircleRadio(e:Event):void
		{
			trace("clickCircleRadio");
			brush = circleBrush;
			setColor();
		}
		
		public function clickSquaresRadio(e:Event):void
		{
			trace("handleColorPicker");
			brush = squareBrush;
			setColor();
		}
		
		public function clickFanRadio(e:Event):void
		{
			trace("clickFanRadio");
			brush = fanBrush;
			setColor();
		}
		
		public function clickRibbonRadio(e:Event):void
		{
			trace("clickRibbonRadio");
			brush = ribbonBrush;
			setColor();
		}
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function setColor():void
		{
			var tBrush:AbstractBrush = brush as AbstractBrush
			if(tBrush)
				tBrush.style.setHexColor(colorPicker.value);
		}
	}
}